package com.yjd.pub.merchant.service;

import com.yjd.comm.app.model.MerchantPushRegidModel;
import com.yjd.comm.app.model.PushRegidModel;
import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.device.model.MerchantDeviceModel;
import com.yjd.comm.device.service.IMerchantDeviceService;
import com.yjd.comm.enums.DeviceStatus;
import com.yjd.comm.merchant.model.MerchantFundsModel;
import com.yjd.comm.merchant.model.MerchantModel;
import com.yjd.comm.merchant.service.IMerchantFundsService;
import com.yjd.comm.merchant.service.IMerchantService;
import com.yjd.comm.user.dto.MerchantSession;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.SecurityCode;
import com.yjd.comm.util.ServletUtil;
import com.yjd.comm.util.StringUtil;
import com.yjd.pub.app.dao.IMerchantPushRegidDao;
import com.yjd.pub.device.dao.IMerchantDeviceDao;
import com.yjd.pub.merchant.dao.IMerchantDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <b>description</b>：商户信息业务实现<br>
 * <b>time</b>：2017-08-11 17:29:51 <br>
 * <b>author</b>：  yangchangyan@yijiedai.com
 */
@Service("merchantService")
public class MerchantServiceImpl extends BaseServiceImpl implements IMerchantService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public MerchantModel insert(MerchantModel model) throws Exception {
		return this.merchantDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(MerchantModel model) throws Exception {
		return this.merchantDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param org_id
	 * @return
	 * @throws Exception
	 */
	public int delete(long org_id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("org_id", org_id);
		return this.merchantDao.deleteModel(paramMap, true);
	}

	@Override
	public int delete(Map paramMap) throws Exception {
		return this.merchantDao.deleteModel(paramMap, true);
	}

	@Override
	public int deleteMerchantAndFundsAccount(MerchantModel merchant) throws Exception {
		this.merchantDao.deleteModel(FrameUtil.newHashMap("org_id", merchant.getOrg_id(), "name", merchant.getName()), true);
		this.merchantFundsService.delete(merchant.getOrg_id());
		return 1;
	}

	@Override
	public MerchantModel login(String username, String password, String loginIp) throws Exception {
		return this.merchantlogin(username, password, loginIp);
	}

	private MerchantModel merchantlogin(String username, String password, String loginIp) throws Exception{
		MerchantModel dbUser = this.merchantDao.getModelOne(FrameUtil.newHashMap("name", username), true);
		if(null == dbUser){
			return null;
		}
		// 用户输入的密码
		String currentPwd = SecurityCode.passwordMD5(password.trim(), dbUser.getEncrypt());
		// 数据库中用户的密码
		String dbpwd = dbUser.getPassword();
		if (dbpwd.equals(currentPwd)) {

			Long currentTime = FrameUtil.getTime();
			MerchantModel updateModel = new MerchantModel();
			updateModel.setOrg_id(dbUser.getOrg_id());
			updateModel.setWhere_version(dbUser.getWhere_version());
			updateModel.setLast_login_ip(loginIp);
			updateModel.setLast_login_time(currentTime);
			updateModel.setUpdate_time(currentTime);
			merchantDao.updateModel(updateModel, true);

			return dbUser;
		}

		return null;
	}


	@Override
	public MerchantModel mobileLogin(String username, String password, Map<String, String> param) throws Exception {
		MerchantModel mModel = this.merchantlogin(username, password, param.get("loginIp"));
		if (null == mModel) {
			return null;
		}

		MerchantDeviceModel device = merchantDeviceDao.getModelOne(
				FrameUtil.newHashMap("org_id", mModel.getOrg_id()), false);

		// 更新设备使用情况
		Long currentTime = FrameUtil.getTime();
		if (null == device) {
			device = new MerchantDeviceModel();
			device.setOrg_id(mModel.getOrg_id());
			device.setLast_login_time(currentTime);
			device.setCreate_time(currentTime);
			device.setLast_update_time(currentTime);
			device.setDevice_name(param.get("device_name"));
			device.setDevice_no(param.get("device_no"));
			device.setInuse(DeviceStatus.enable.getCode());
			merchantDeviceDao.insertModel(device, true);
		} else {
			device.setWhere_version(device.getVersion());
			device.setLast_login_time(currentTime);
			device.setLast_update_time(currentTime);
			device.setDevice_name(param.get("device_name"));
			device.setDevice_no(param.get("device_no"));
			device.setInuse(DeviceStatus.enable.getCode());
			merchantDeviceDao.updateModel(device, true);
		}

		//记录极光推送id
		if (StringUtils.isNotEmpty(param.get("regid"))) {
			MerchantPushRegidModel regidModel = merchantPushRegidDao.getModelOne(
					FrameUtil.newHashMap("org_id", mModel.getOrg_id()), false);
			if (null == regidModel) {
				regidModel = new MerchantPushRegidModel();
				regidModel.setOrg_id(mModel.getOrg_id());
				regidModel.setRegid(param.get("regid"));
				regidModel.setCreate_time(currentTime);
				merchantPushRegidDao.insertModel(regidModel, true);
			} else {
				regidModel.setRegid(param.get("regid"));
				regidModel.setCreate_time(currentTime);
				merchantPushRegidDao.updateModel(regidModel, true);
			}
		}

		return mModel;
	}

	@Override
	public void mobileLogout(Long org_id, String device_no) throws Exception {
		if (StringUtil.isNotEmpty(org_id)) {
			MerchantPushRegidModel regidModel = merchantPushRegidDao.getModelOne(FrameUtil.newHashMap("org_id", org_id), false);
			if (null != regidModel) {
				merchantPushRegidDao.deleteById(regidModel.getId(), true);
			}

			MerchantDeviceModel device = merchantDeviceDao.getModelOne(FrameUtil.newHashMap(
					"org_id", org_id, "device_no", device_no), false);

			// 更新设备使用情况
			Long currentTime = FrameUtil.getTime();
			if (null != device) {
				device.setWhere_version(device.getVersion());
				device.setLast_login_time(currentTime);
				device.setLast_update_time(currentTime);
				device.setInuse(DeviceStatus.unable.getCode());
				merchantDeviceDao.updateModel(device, true);
			}

			MerchantModel mModel = new MerchantModel();
			mModel.setOrg_id(org_id);
			mModel.setWhere_version(mModel.getVersion());
			Long current = FrameUtil.getTime();
			mModel.setUpdate_time(current);
			mModel.setLast_login_time(current);
			merchantDao.updateModel(mModel, true);
		}
	}

	/**
	 * 开户
	 *
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@Override
	public MerchantModel openMerchant(MerchantModel model) throws Exception {
		if(model==null){
			FrameUtil.throwBaseException1("请求参数不能为空！");
		}
		Long currtime = FrameUtil.getTime(null);
		MerchantFundsModel fundsModel = new MerchantFundsModel();
		fundsModel.setOrg_id(model.getOrg_id());
		fundsModel.setCreate_time(currtime);
		fundsModel.setUpdate_time(currtime);
		merchantFundsService.insert(fundsModel);

		model.setCreate_time(currtime);
		model.setUpdate_time(currtime);
		return merchantDao.insertModel(model,true);
	}

	@Override
	public MerchantSession getMerchantSession(Long id) {
		return this.merchantDao.getMerchantSession(FrameUtil.newHashMap("org_id", id));
	}

	@Override
	public MerchantSession getMerchantMobileSession(Long id) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("org_id", id);
		paramMap.put("inuse", DeviceStatus.enable.getCode());
		return this.merchantDao.getMerchantMobileSession(paramMap);
	}

	@Resource
	private IMerchantDao merchantDao;

	@Resource
	private IMerchantFundsService merchantFundsService;

	@Resource
	private IMerchantDeviceDao merchantDeviceDao;

	@Resource
	private IMerchantPushRegidDao merchantPushRegidDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.merchantDao;
	}

}
